package com.koron.ticket.workchange.ticketchangemember.service.impl;


import com.koron.bean.base.Response;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.workchange.ticketchangemember.bean.ChanageMemberDet;
import com.koron.ticket.workchange.ticketchangemember.bean.TicketChangeMember;
import com.koron.ticket.workchange.ticketchangemember.bean.TicketChangeMemberQuery;
import com.koron.ticket.workchange.ticketchangemember.mapper.ChanageMemberDetMapper;
import com.koron.ticket.workchange.ticketchangemember.mapper.TicketChangeMemberMapper;
import com.koron.ticket.workchange.ticketchangemember.service.TicketChangeMemberService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (TicketChangeMember)表服务实现类
 *
 * @author 朱超
 * @since 2021-10-27 10:19:49
 */
@Service
public class TicketChangeMemberServiceImpl implements TicketChangeMemberService {

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("insert")
    public Response insert(SessionFactory factory, TicketChangeMember ticketChangeMember) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        ChanageMemberDetMapper chanagemapper = factory.getMapper(ChanageMemberDetMapper.class);
        String id = CodeTools.getCode32();
        ticketChangeMember.setId(id);
        mapper.insert(ticketChangeMember);
        List<ChanageMemberDet> det = ticketChangeMember.getDet();
        det.stream().forEach(d->{
            d.setId(CodeTools.getCode32());
            d.setChanageId(id);
        });
        chanagemapper.batchInsert(det);
        return Response.success(id);
    }

    /**
     * 根据id删除单条数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public Response deleteById(SessionFactory factory, String id) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        mapper.deleteById(id);
        return Response.success("删除成功");
    }

    /**
     * 根据workTicketId删除相关数据
     *
     */
    @Override
    public void deleteByWorkTicketId(SessionFactory factory, String workTicketId) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        mapper.deleteByWorkTicketId(workTicketId);
    }

    /**
     * 更新数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public Response update(SessionFactory factory, TicketChangeMember ticketChangeMember) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        mapper.update(ticketChangeMember);
        return Response.success("更新成功");
    }

    /**
     * 根据id查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public Response getById(SessionFactory factory, String id) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        TicketChangeMemberQuery query = new TicketChangeMemberQuery();
        query.setId(id);
        TicketChangeMember ticketChangeMember = mapper.getSingle(query);
        return Response.success(ticketChangeMember);
    }

    @Override
    public TicketChangeMember getSingle(SessionFactory factory, TicketChangeMemberQuery ticketChangeMemberQuery) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        return mapper.getSingle(ticketChangeMemberQuery);
    }

    @Override
    public List<TicketChangeMember> getList(SessionFactory factory, TicketChangeMemberQuery ticketChangeMemberQuery) {
        TicketChangeMemberMapper mapper = factory.getMapper(TicketChangeMemberMapper.class);
        return mapper.getList(ticketChangeMemberQuery);
    }

}
